<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('团队成员') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('用户名称')" prop="userName">
        <el-input v-model="queryParams.userName" :placeholder="$t('用户名称')" />
      </el-form-item>
      <el-form-item :label="$t('参与角色')" prop="role">
        <el-select v-model="queryParams.role" :placeholder="$t('参与角色')" clearable>
          <el-option
            v-for="dict in roleOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('参与时间')" prop="joinTime">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />

      </el-form-item>
      <el-form-item :label="$t('是否离开')" prop="leave">
        <el-select v-model="queryParams.leave" :placeholder="$t('是否离开')" clearable>
          <el-option
            v-for="dict in leaveOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:team:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:team:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>-->
    </el-row>

    <el-table ref="table" v-loading="loading" :data="fundTeamList" border @cell-mouse-enter.once="rowDrop">
      <el-table-column label="" align="center" width="50">
        <div class="handle">
          <i class="el-icon-rank link-type" />
        </div>
      </el-table-column>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('用户名称')" align="center" prop="userName">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('参与角色')" align="center" prop="role" :formatter="roleFormat" />
      <el-table-column :label="$t('参与时间')" align="center" prop="joinTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否离开')" align="center" prop="leave" :formatter="leaveFormat" />
      <el-table-column :label="$t('离开时间')" align="center" prop="leaveTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['fund:team:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['fund:team:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基金团队成员对话框 -->
    <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <fundTeamForm ref="fundTeamForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listFundTeam, delFundTeam, saveTeamList } from '@/api/fund/team/fundTeam'
import fundTeamForm from './fundTeamForm.vue'
import { downLoadExcel } from '@/utils/excelDownload'
import Sortable from 'sortablejs'
export default {
    components: {
        fundTeamForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 基金团队成员表格数据
          fundTeamList: [],
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
        // 参与角色 字典fund_team_role字典
        roleOptions: [],
        // 是否离开 字典 sys_yes_no字典
        leaveOptions: [],
        // 时间范围参数
        dateRange: [],
          // 查询参数
          queryParams: {
              fundId: this.$route.query.id,
              userId: undefined,
              role: undefined,
              joinTime: undefined,
              leave: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 保存列表参数
          saveTeamListParam: {
            pageNum: undefined,
            pageSize: undefined,
            list: []
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('fund_team_role').then(response => {
          this.roleOptions = response.data
      })
      this.getDicts('sys_yes_no').then(response => {
          this.leaveOptions = response.data
      })
  },
  methods: {
    /** 查询基金团队成员列表 */
    getList() {
        this.loading = true
        listFundTeam(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.fundTeamList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 参与角色 字典fund_team_role字典翻译
    roleFormat(row) {
      return this.selectDictLabel(this.roleOptions, row.role)
    },
    // 是否离开 字典 sys_yes_no字典翻译
    leaveFormat(row) {
      return this.selectDictLabel(this.leaveOptions, row.leave)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            userId: undefined,
            fundId: undefined,
            role: undefined,
            joinTime: undefined,
            leave: undefined,
            leaveTime: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.dateRange = undefined
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('添加基金团队成员')
        this.$nextTick(() => {
            this.$refs.fundTeamForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('修改基金团队成员')
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.fundTeamForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delFundTeam(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
       downLoadExcel('/team/fundTeam/export', this.queryParams)
    },
    rowDrop() {
      // 行拖拽
      const _this = this
      const tbody2 = this.$refs.table.$el.querySelector('.el-table__body-wrapper tbody')
      Sortable.create(tbody2, {
        handle: '.handle',
        animation: 500,
        fallbackOnBody: true,
        onChoose() {
          // 选择元素
          _this.fundTeamList[_this.fundTeamList.length - 1].fixed = false
        },
        onUnchoose: function(evt) {
          // 取消选择元素
          if (evt) {
            _this.fundTeamList[_this.fundTeamList.length - 1].fixed = 'right'
          }
        },
        onEnd(evt) {
          _this.fundTeamList.splice(evt.newIndex, 0, _this.fundTeamList.splice(evt.oldIndex, 1)[0])
          var newArray = _this.fundTeamList.slice(0)
          _this.fundTeamList = []
          _this.$nextTick(() => {
            _this.fundTeamList = newArray
            _this.fundTeamList.forEach((item, index) => {
              item.rowSort = index + 1
            })
            _this.saveTeamList(_this.fundTeamList)
          })
        }
      })
      this.$forceUpdate()
    },
    saveTeamList(fundTeamList) {
      this.saveTeamListParam.list = fundTeamList
      this.saveTeamListParam.pageNum = this.queryParams.pageNum
      this.saveTeamListParam.pageSize = this.queryParams.pageSize
      saveTeamList(this.saveTeamListParam).then(response => {
        if (response.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
        }
      })
    }
  }
}
</script>
